package com.molyfun.parents.activity.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.fragment.dialog.CommonChooseDialogFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.utils.ActivityManager;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/23/16.
 */
public class DebugModeActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.offical_server)
    TextView mOfficalServer;
    @InjectView(R.id.pre_server)
    TextView mPreServer;
    @InjectView(R.id.test_server)
    TextView mTestServer;
    @InjectView(R.id.base_title)
    TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug_mode);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("调试模式");

        switch (PreferencesStorageUtil.getServerMode(DebugModeActivity.this)) {
            case PreferencesStorageUtil.ServerMode.TEST:
                mOfficalServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                mPreServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                mTestServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.color_title_line));
                break;

            case PreferencesStorageUtil.ServerMode.PREPARE:
                mPreServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.color_title_line));
                mOfficalServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                mTestServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                break;

            case PreferencesStorageUtil.ServerMode.OFFICAL:
            default:
                mOfficalServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.color_title_line));
                mPreServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                mTestServer.setBackgroundColor(ContextCompat.getColor(DebugModeActivity.this, R.color.text_color_normal));
                break;
        }

        mBack.setOnClickListener(this);
        mOfficalServer.setOnClickListener(this);
        mTestServer.setOnClickListener(this);
        mPreServer.setOnClickListener(this);
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, DebugModeActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.offical_server:
                new CommonChooseDialogFragment.CommonChooseDialogBuilder("需要重新登录以便生效，是否继续？")
                        .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                            @Override
                            public void onConfirm() {
                                PreferencesStorageUtil.setServerMode(DebugModeActivity.this, PreferencesStorageUtil.ServerMode.OFFICAL);
                                PreferencesStorageUtil.logout(getApplicationContext());
                                Intent intent = new Intent(DebugModeActivity.this, LoginActivity.class);
                                if (isFinishing()){
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                }
                                startActivity(intent);
                                ActivityManager.getInstance().logout();
                                RestClient.refreshBaseUrl();
                                MFPApplication.getInstance().refreshRestClient("");
                            }

                            @Override
                            public void onCancel() {

                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());
                break;
            case R.id.pre_server:
                new CommonChooseDialogFragment.CommonChooseDialogBuilder("需要重新登录以便生效，是否继续？")
                        .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                            @Override
                            public void onConfirm() {
                                PreferencesStorageUtil.setServerMode(DebugModeActivity.this, PreferencesStorageUtil.ServerMode.PREPARE);
                                PreferencesStorageUtil.logout(getApplicationContext());
                                Intent intent = new Intent(DebugModeActivity.this, LoginActivity.class);
                                if (isFinishing()){
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                }
                                startActivity(intent);
                                ActivityManager.getInstance().logout();
                                RestClient.refreshBaseUrl();
                                MFPApplication.getInstance().refreshRestClient("");
                            }

                            @Override
                            public void onCancel() {

                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());
                break;
            case R.id.test_server:
                new CommonChooseDialogFragment.CommonChooseDialogBuilder("需要重新登录以便生效，是否继续？")
                        .setCommonChooseDialogClickListener(new CommonChooseDialogFragment.CommonChooseDialogClickListener() {
                            @Override
                            public void onConfirm() {
                                PreferencesStorageUtil.setServerMode(DebugModeActivity.this, PreferencesStorageUtil.ServerMode.TEST);
                                PreferencesStorageUtil.logout(getApplicationContext());
                                Intent intent = new Intent(DebugModeActivity.this, LoginActivity.class);
                                if (isFinishing()){
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                }
                                startActivity(intent);
                                ActivityManager.getInstance().logout();
                                RestClient.refreshBaseUrl();
                                MFPApplication.getInstance().refreshRestClient("");
                            }

                            @Override
                            public void onCancel() {

                            }
                        })
                        .build(getSupportFragmentManager().beginTransaction());
                break;
            default:
                break;
        }
    }
}